//-----------------------------------------------------------------------
// <copyright file="tblInternalRateDAOBase.cs" company="Scb">
//     Copyright ? Scb. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using Scb.Framework;
using Scb.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Robot.Entities;

namespace Robot.Data
{    
    public abstract class tblInternalRateDAOBase
    {
        #region Common methods
        public virtual tblInternalRate CreatetblInternalRateFromReader(IDataReader reader)
        {
            tblInternalRate item = new tblInternalRate();
            try
            {
                if (UtilityDAO.CheckColumnData(reader,"ID")) item.ID = (Guid)reader["ID"];
                if (UtilityDAO.CheckColumnData(reader,"BuyCCY")) item.BuyCCY = (string)reader["BuyCCY"];
                if (UtilityDAO.CheckColumnData(reader,"FloorRate")) item.FloorRate = (decimal)reader["FloorRate"];
                if (UtilityDAO.CheckColumnData(reader,"SellCCY")) item.SellCCY = (string)reader["SellCCY"];
                if (UtilityDAO.CheckColumnData(reader,"CeilingRate")) item.CeilingRate = (decimal)reader["CeilingRate"];
                if (UtilityDAO.CheckColumnData(reader,"InternalRate")) item.InternalRate = (decimal)reader["InternalRate"];
                if (UtilityDAO.CheckColumnData(reader,"FromFileID")) item.FromFileID = (Guid)reader["FromFileID"];
                if (UtilityDAO.CheckColumnData(reader,"CreatedDate")) item.CreatedDate = (DateTime)reader["CreatedDate"];
                if (UtilityDAO.CheckColumnData(reader,"CreatedBy")) item.CreatedBy = (string)reader["CreatedBy"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreatetblInternalRateFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreatetblInternalRate methods
            
        public virtual void CreatetblInternalRate(tblInternalRate tblInternalRate)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblInternalRateInsert");
                
                database.AddInParameter(dbCommand, "@BuyCCY", DbType.AnsiString, tblInternalRate.BuyCCY);
                database.AddInParameter(dbCommand, "@FloorRate", DbType.Decimal, tblInternalRate.FloorRate);
                database.AddInParameter(dbCommand, "@SellCCY", DbType.AnsiString, tblInternalRate.SellCCY);
                database.AddInParameter(dbCommand, "@CeilingRate", DbType.Decimal, tblInternalRate.CeilingRate);
                database.AddInParameter(dbCommand, "@InternalRate", DbType.Decimal, tblInternalRate.InternalRate);
                database.AddInParameter(dbCommand, "@FromFileID", DbType.Guid, tblInternalRate.FromFileID);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, tblInternalRate.CreatedDate);
                database.AddInParameter(dbCommand, "@CreatedBy", DbType.AnsiString, tblInternalRate.CreatedBy);
                database.AddOutParameter(dbCommand, "@ID", DbType.Guid, 16);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                tblInternalRate.ID = (Guid)database.GetParameterValue(dbCommand, "@ID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreatetblInternalRateException, ex);
            }
        }

        #endregion

        #region UpdatetblInternalRate methods
        
        public virtual void UpdatetblInternalRate(tblInternalRate tblInternalRate)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblInternalRateUpdate");            
                
                database.AddInParameter(dbCommand, "@ID", DbType.Guid, tblInternalRate.ID);
                database.AddInParameter(dbCommand, "@BuyCCY", DbType.AnsiString, tblInternalRate.BuyCCY);
                database.AddInParameter(dbCommand, "@FloorRate", DbType.Decimal, tblInternalRate.FloorRate);
                database.AddInParameter(dbCommand, "@SellCCY", DbType.AnsiString, tblInternalRate.SellCCY);
                database.AddInParameter(dbCommand, "@CeilingRate", DbType.Decimal, tblInternalRate.CeilingRate);
                database.AddInParameter(dbCommand, "@InternalRate", DbType.Decimal, tblInternalRate.InternalRate);
                database.AddInParameter(dbCommand, "@FromFileID", DbType.Guid, tblInternalRate.FromFileID);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, tblInternalRate.CreatedDate);
                database.AddInParameter(dbCommand, "@CreatedBy", DbType.AnsiString, tblInternalRate.CreatedBy);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdatetblInternalRateException, ex);
            }
        }
        
        #endregion

        #region DeletetblInternalRate methods
        public virtual void DeletetblInternalRate(Guid id)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblInternalRateDelete");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Guid, id);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeletetblInternalRateException, ex);
            }
        }

        #endregion

        #region GettblInternalRate methods
        
        public virtual tblInternalRate GettblInternalRate(Guid id)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblInternalRateGet");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Guid, id);
                
                tblInternalRate tblInternalRate =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        tblInternalRate = CreatetblInternalRateFromReader(reader);
                        reader.Close();
                    }
                }
                return tblInternalRate;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGettblInternalRateException, ex);
            }
        }
        
        #endregion

        #region GettblInternalRateList methods
        public virtual tblInternalRateCollection GettblInternalRateList(tblInternalRateColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblInternalRateGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                tblInternalRateCollection tblInternalRateCollection = new tblInternalRateCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        tblInternalRate tblInternalRate = CreatetblInternalRateFromReader(reader);
                        tblInternalRateCollection.Add(tblInternalRate);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return tblInternalRateCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGettblInternalRateListException, ex);
            }
        }
        
        public virtual tblInternalRateCollection GettblInternalRateList(tblInternalRateColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GettblInternalRateList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}